class AddRightsToEmployeesForAdmins < ActiveRecord::Migration
  
  def self.create_right(name, controller, action)
    Right.create :name => name, :controller => controller, :action => action
  end
  
  def self.up
    rights = {
      :employees => {
        :list => create_right('Listaa', 'employees', 'list'),
        :show => create_right('Näytä', 'employees', 'show'),
        :edit => create_right('Muokkaa', 'employees', 'edit'),
        :update => create_right('Tallenna muutokset', 'employees', 'update'),
        :delete => create_right('Poista', 'employees', 'destroy'),
        :new => create_right('Uusi', 'employees', 'new'),
        :create => create_right('Tallenna', 'employees', 'create'),
      }
    }
    roles = {:admin => Role.find_by_name('Järjestelmänvalvoja')}
    roles[:admin].rights << rights[:employees][:show]
    roles[:admin].rights << rights[:employees][:list]
    roles[:admin].rights << rights[:employees][:new]
    roles[:admin].rights << rights[:employees][:create]
    roles[:admin].rights << rights[:employees][:edit]
    roles[:admin].rights << rights[:employees][:update]
    roles[:admin].rights << rights[:employees][:delete]
  end
  
  def self.down
    Right.find(:all, :conditions => "controller = 'employees'").each do |right|
      RoleRight.find(:all, :conditions => "right_id = #{right.id}").each do |role_right|
        role_right.destroy
      end
      right.destroy
    end
  end
end
